var fs = require('fs')
var QrCode = require('qrcode-reader')
var QrcodeTerminal = require('qrcode-terminal')
var Jimp = require("jimp")
var puppeteer = require("puppeteer")

var appjs = fs.readFileSync('app.js', 'utf-8')
var ddjs = fs.readFileSync('dd.js', 'utf-8')

puppeteer.launch().then(browser => {
  browser.newPage().then(page => {
    page.on('console', msg => console.log(msg.text))
    page.setRequestInterception(true).then(() => {
      page.on("request", request => {
        if(request.url.endsWith('app.js')){
          request.respond({
            status: 200,
            contentType: 'application/javascript',
            body: appjs+ddjs
          })
        }else{
          request.continue()
        }
      })
    })
    page.exposeFunction('$XDD_qrcode', text => {
      QrcodeTerminal.generate(text)
    })
    page.goto('https://im.dingtalk.com').then(response => {
    })
  })
})
